<?php

namespace App\Http\Controllers\Api;

use App\Utils\ResponseUtils;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Validator;

class TvController extends BaseController
{
    private $tvs = [
        'A1' => 0,
        'A2' => 1,
        'A3' => 2,
    ];

    public function status(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'SN' => 'required',
        ], [
            'SN.required' => 'SN不能为空',
            'token.required' => 'token不能为空',
        ]);
        $error = [
            'error' => ''
        ];
        if ($validator->fails()) {
            $error['error'] = $validator->errors()->first();
            return $error;
        }

        $sn = $request->get('SN');
        $token = $request->get('token');

        $path = base_path();
        $priKey = file_get_contents($path . '/secret1/' . 'rsa_private_key.pem');

        // 私钥解密
        $privateKey = openssl_pkey_get_private($priKey);
        if(!$privateKey){ // 私钥不可用
            $error['error'] = '私钥不可用';
            return $error;
        }
        $result = openssl_private_decrypt(base64_decode($token), $decrypted, $privateKey, OPENSSL_NO_PADDING);
        if(!$result){ // 解密失败
            $error['error'] = '解密失败';
            return $error;
        }
        if (time() * 1000 - trim($decrypted) > 300000) {
            $error['error'] = '消息已过期';
            return $error;
        }

        if (!isset($this->tvs[$sn])) {
            $error['error'] = '设备不存在';
            return $error;
        }

        $data = [
            'stage' => 0, // 0，设备未绑定状态；1，设备已绑定未充值状态；2，设备已绑定已充值状态
            'lockStatus' => true, // true，锁机；false，解锁
            'targetRentTime' => Carbon::now()->format('Ymd'), //deadline，年月日格式
            'Timestamp' => Carbon::now()->format('Ymd'), //服务端当前时间戳：年月日格式
        ];
        $stage = $this->tvs[$sn];
        $data['stage'] = $stage;
        switch ($stage) {
//            case 0:
//                $data['lockStatus'] = true;
//                $data['targetRentTime'] = Carbon::now()->format('Ymd');
//                $data['Timestamp'] = Carbon::now()->format('Ymd');
//                break;
//            case 1:
//                $data['lockStatus'] = true;
//                $data['targetRentTime'] = Carbon::now()->format('Ymd');
//                $data['Timestamp'] = Carbon::now()->format('Ymd');
//                break;
            case 2:
                $data['lockStatus'] = false;
                $data['targetRentTime'] = Carbon::now()->addDays(1)->format('Ymd');
                $data['Timestamp'] = Carbon::now()->format('Ymd');
                break;
        }
        return $data;
    }

    public function test()
    {
        $path = base_path();
        $pubKey = file_get_contents($path . '/secret1/' . 'rsa_public_key.pem');
        $priKey = file_get_contents($path . '/secret1/' . 'rsa_private_key.pem');
        var_dump($path);
        //公钥加密
        $public_key = openssl_pkey_get_public($pubKey);
        if(!$public_key){
            die('公钥不可用');
        }
        //第一个参数是待加密的数据只能是string，第二个参数是加密后的数据,第三个参数是openssl_pkey_get_public返回的资源类型,第四个参数是填充方式
        $return_en = openssl_public_encrypt(str_pad(time() * 1000, 128), $crypted, $public_key, OPENSSL_NO_PADDING);
        if(!$return_en){
            return('加密失败,请检查RSA秘钥1');
        }
        $eb64_cry = base64_encode($crypted);
        echo "公钥加密数据：".$eb64_cry;
        echo "<hr>";

        //私钥解密
        $private_key = openssl_pkey_get_private($priKey);
        if(!$private_key){
            die('私钥不可用');
        }
        $return_de = openssl_private_decrypt(base64_decode($eb64_cry), $decrypted, $private_key, OPENSSL_NO_PADDING);
        if(!$return_de){
            return('解密失败,请检查RSA秘钥2');
        }
        echo "私钥解密数据:".$decrypted;
        echo "<hr>";
    }
}
